package com.bjpowernode.listener;

import com.bjpowernode.pojo.ProductType;
import com.bjpowernode.service.ProductTypeService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

@WebListener
public class ProductTypeListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
    //因web.xml中配置的ContextLoaderListener监听器也实现的是ServletContextListener接口
    //   所以并不知道这两个监听器哪个先创建。因此需手动获取ProductTypeServiceImpl的对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_*.xml");
     ProductTypeService productTypeService = (ProductTypeService) context.getBean("ProductTypeServiceImpl");
        List<ProductType> typeList = productTypeService.getAll();
        //将获取的全部商品类型放在全局作用域中，供新增，修改，查询等页面使用
        servletContextEvent.getServletContext().setAttribute("typeList",typeList );
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
